/**
 * 电梯风险监测统计看板
 */
odoo.define('lift_risk.risk_event_line', function (require) {
    "use strict";
    var ajax = require('web.ajax');
    var core = require('web.core');
    var Widget = require('web.Widget');
    var Model = require('web.Model');
    var session = require('web.session');
    var framework = require('web.framework');
    var form_common = require('web.form_common');
    var ListView = require("web.ListView");
    var _t = core._t;


    // 事件列表widget
    var RiskEventLineListWidget = form_common.FormWidget.extend({
        template:"risk_event_line_list",
        init: function(field_manager, node) {
            this._super(field_manager, node);
            this.field_name = node.attrs.field_name;
            this.event_code = node.attrs.event_code.split(",");
        },
        start:function(){
            this._super();
            this.get_date();
            this.show_lines();
            this.field_manager.on("field_changed:" + this.field_name,this,this.show_lines);
        },
        get_date: function () {
            // 设置上月第一天和最后一天
            var current_date = new Date();
            var year = current_date.getFullYear();
            var month = current_date.getMonth();
            if(month == 0){
                month=12;
                year=year-1;
            } else if (month < 10) {
                month = "0" + month;
            }
            this.first_date = year + "-" + month + "-" + "01";
            var temp_date = new Date(year, month, 0);
            this.last_date = year + "-" + month + "-" + temp_date.getDate();
        },
        show_lines: function() {
            var self = this;
            var $table = self.$(".risk_line_tbody");
            $table.empty();
            self.field_name_id = self.field_manager.get_field_value(self.field_name);
            var domain = [
                [self.field_name, '=', self.field_name_id],
                ['date','<=',self.last_date],
                ['date','>=',self.first_date]
            ];
            self.event_code.map(function (item) {
                if (item.length == 1){
                    domain.push(item)
                }else {
                    domain.push(['event_code', '=', item])
                }
            });

            var limit = 10;
            new Model("tyibs.risk.event.line").query(['date','event_category_id','res_model','res_id']).filter(domain).limit(limit + 1).all().then(function (res) {
                res.forEach(function (item, index) {
                    if (index < limit){
                        var line_item = new RiskEventLineItemWidget(self, index + 1, item.date, item.event_category_id[1], item.res_model, item.res_id)
                        line_item.appendTo($table);
                    }else {
                        var read_more = new RiskEventLineReadMoreWidget(self, "tyibs.risk.event.line", domain);
                        read_more.appendTo($table);
                    }
                });
            })
        }
    });

    // 查看更多
    var RiskEventLineReadMoreWidget = Widget.extend({
        template: "risk_event_line_read_more",
        init: function (parent,res_model,domain) {
            this._super(parent);
            this.res_model = res_model;
            this.domain = domain;
        },
        events:{
            'click .tyibs_read_more': "tyibs_read_more",
        },
        tyibs_read_more: function () {
            this.do_action({
                name: "事件列表",
                type: 'ir.actions.act_window',
                res_model: this.res_model,
                views: [[false, 'list'],[false, 'form']],
                domain: this.domain,
                target: 'current',
                context: {},
            });
        }
    });

    // 事件行item
    var RiskEventLineItemWidget = Widget.extend({
        template: "risk_event_line_item",
        init: function (parent,index,date,event,res_model,res_id) {
            this._super(parent);
            this.index = index;
            this.date = date;
            this.event = event;
            this.res_model = res_model;
            this.res_id = res_id;
        },
        events:{
            'click .show_detail': "show_detail",
        },
        show_detail: function () {
            var self = this;
            self.do_action({
                name: self.event,
                type: 'ir.actions.act_window',
                res_model: self.res_model,
                res_id: self.res_id * 1,
                views: [[false, 'form']],
                target: 'current',
                context: {readonly:1},
            });
            return false;
        }
    });

    core.form_tag_registry.add('risk_event_line_list_widget', RiskEventLineListWidget);

    return {
        RiskEventLineListWidget: RiskEventLineListWidget,
    };
});